// pages/addAdress/addAdress.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    sexList:[
      {name: '先生', value: 0,checked:'true' },
      {name: '女士', value: 1 }
    ],
    addrIcon:[
      { name: '公司', value: 1, dis: 0 },
      { name: '家', value: 2, dis: 0 },
      { name: '学校', value: 3, dis: 0 }
    ],
    postData:{
      mysex:0
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
  
  },
  //收货人姓名
  myName:function(e){
    var val = e.detail.value;
    var postData = this.data.postData;
    postData.myName = val;
    this.setData({
      postData: postData
    })
  },
  // 点击选择性别
  radioChange:function(e){
    var val = e.detail.value;
    var postData = this.data.postData;
    postData.mysex = val;
    this.setData({
      postData: postData
    })
  },
  //点击选择地区
  choosePosition:function(e){
    var that = this;
    wx.chooseLocation({
      // type: 'gcj02', //返回可以用于wx.openLocation的经纬度
      success: function (res) {
        console.log(res)
        var positionTxt = res.name;
        var postData = that.data.postData;
        postData.positionTxt = positionTxt;
        that.setData({
          postData: postData
        })
      }
    })
    
  },
  //输入手机号码
  phoneNum:function(e){
    var val = e.detail.value;
    var postData = this.data.postData;
    postData.phoneNum = val;
    this.setData({
      postData: postData
    })
  },
  //输入详细地址
  myLocation:function(e){
    var val = e.detail.value;
    var postData = this.data.postData;
    postData.myLocation = val;
    this.setData({
      postData: postData
    })
  },
  // 点击补充说明
  chooseAddrIcon:function(e){
    var val = e.currentTarget.dataset.val;
    var dis = e.currentTarget.dataset.dis;
    var addIconData = this.data.addrIcon;
    var postData = this.data.postData;
    postData.supply = val;
    for (var i = 0; i < addIconData.length;i++){
      addIconData[i].dis = 0;
    }
    addIconData[dis].dis = 1;
    this.setData({
      addrIcon: addIconData,
      postData: postData
    })
  },
  //点击删除
  delAddr:function(e){
    wx.showModal({
      title: '删除地址',
      content: '确认是否要删除该地址吗？',
      success: function (res) {
        if (res.confirm) {
          console.log('用户点击确定')
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  // 点击确定
  addrSave:function(e){
    var postData = this.data.postData
    //判定姓名、电话、地区不能为空
    if (postData.myName == '' || postData.myName == null){
      wx.showToast({
        title: '请填写姓名',
        icon: 'none',
        duration: 1500
      })
    } else if (postData.phoneNum == '' || postData.phoneNum == null) {
      wx.showToast({
        title: '请填写联系电话',
        icon: 'none',
        duration: 1500
      })
    } else if (postData.positionTxt == '' || postData.positionTxt == null) {
      wx.showToast({
        title: '请选择地区',
        icon: 'none',
        duration: 1500
      })
    }else{
      console.log(postData)
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})